package com.hf.eagle.web.struts;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.Accessory;
import com.hf.eagle.domain.ProductInfo;
import com.hf.eagle.domain.SysDictionary;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * 产品展示
 * 
 * @author xiaokeheng
 */
public class ProductDisplayAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.view(mapping, form, request, response);
	}

	/**
	 * 显示产品展示
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 根据二级查询//ProductInfo 产品型号名称 及图片信息
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		Pager pager = (Pager) dynaBean.get("pager");
		pager.setPageSize(8);
		ProductInfo productInfo = new ProductInfo();
		productInfo.setIs_del(0);

		Long recordCount = super.getFacade().getProductInfoService().getProductInfoCount(productInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		productInfo.getRow().setFirst(pager.getFirstRow());
		productInfo.getRow().setCount(pager.getLastRow());

		List<ProductInfo> list = super.getFacade().getProductInfoService().getProductInfoPaginatedList(productInfo);
		for (ProductInfo productInfo2 : list) {
			Accessory acce = new Accessory();
			acce.setFile_level(SysConstant.MAIN_IMG);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", productInfo2.getProduct_id());
			acce = super.getFacade().getAccessoryService().getAccessory(acce);
			productInfo2.setAccessory(acce);
		}

		request.setAttribute("entityList", list);
		return mapping.findForward("view");
	}

	/**
	 * 显示产品展示列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// SysDictionary 查询一级 0 PRODUCT_FIRST_TYPE 4000
		// 根据一级查二级 par_id 一起封装LIST
		String par_id = "0";
		String dictionary_id = String.valueOf(SysConstant.PRODUCT_FIRST_TYPE);

		SysDictionary productType = new SysDictionary();
		productType.setPar_id(Long.valueOf(par_id));
		productType.setDictionary_id(Long.valueOf(dictionary_id));
		productType.setIs_del(0);

		List<SysDictionary> typeList = super.getFacade().getSysDictionaryService().getSysDictionaryList(productType);
		for (SysDictionary sysDictionary : typeList) {
			Long id = sysDictionary.getId();
			SysDictionary secondProductType = new SysDictionary();
			secondProductType.setPar_id(id);
			secondProductType.setDictionary_id(SysConstant.PRODUCT_SECOND_TYPE);
			secondProductType.setIs_del(0);
			List<SysDictionary> secondTypeList = super.getFacade().getSysDictionaryService().getSysDictionaryList(
					secondProductType);
			sysDictionary.setSysDictionaryList(secondTypeList);
		}
		request.setAttribute("typeList", typeList);

		// 根据二级查询//ProductInfo 产品型号名称 及图片信息
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		String type1_id = (String) dynaBean.get("type1_id");
		String type2_id = (String) dynaBean.get("type2_id");
		String type1_name = (String) dynaBean.get("type1_name");
		String type2_name = (String) dynaBean.get("type2_name");
		String is_recommend = (String) dynaBean.get("is_recommend");
		String title = (String) dynaBean.get("title");
		Pager pager = (Pager) dynaBean.get("pager");
		pager.setPageSize(8);
		ProductInfo productInfo = new ProductInfo();
		productInfo.setIs_del(0);
		if (StringUtils.isNotBlank(is_recommend) && "1".equals(is_recommend)) {
			productInfo.setIs_recommend(Integer.parseInt(is_recommend));
			title = "<a title=\"店长推荐产品\" href=\"ProductDisplay.do?method=list&mod_id=3200&is_recommend=1\">店长推荐产品</a>";
		} else if (StringUtils.isNotBlank(type1_id) && StringUtils.isBlank(type2_id)) {
			productInfo.getMap().put("type1_id", type1_id);
			if (StringUtils.isBlank(type1_name)) {
				SysDictionary sd = new SysDictionary();
				sd.setDictionary_id(4000l);
				sd.setDictionary_value(Integer.valueOf(type1_id));
				sd.setIs_del(0);
				sd = super.getFacade().getSysDictionaryService().getSysDictionary(sd);
				if (null != sd) {
					type1_name = sd.getDictionary_desc_cn();
				}
			}
			title = "<a title='" + type1_name + "' href='ProductDisplay.do?method=list&mod_id=3200&type1_id="
					+ type1_id + "&type1_name=" + type1_name + "'>" + type1_name + "</a>";
		} else if (StringUtils.isNotBlank(type1_id) && StringUtils.isNotBlank(type2_id)) {
			productInfo.setProduct_type(Integer.parseInt(type2_id));
			title = "<a title='" + type1_name + "' href='ProductDisplay.do?method=list&mod_id=3200&type1_id="
					+ type1_id + "&type1_name=" + type1_name + "'>" + type1_name + "</a>&gt;&gt;<a title='"
					+ type2_name + "' href='ProductDisplay.do?method=list&mod_id=3200&type1_id=" + type1_id
					+ "&type1_name=" + type1_name + "&type2_id=" + type2_id + "&type2_name=" + type2_name + "'>"
					+ type2_name + "</a>";
		}
		// super.copyProperties(productInfo, form);

		Long recordCount = super.getFacade().getProductInfoService().getProductInfoCount(productInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		productInfo.getRow().setFirst(pager.getFirstRow());
		productInfo.getRow().setCount(pager.getLastRow());
		List<ProductInfo> list = super.getFacade().getProductInfoService().getProductInfoPaginatedList(productInfo);
		for (ProductInfo productInfo2 : list) {
			Accessory acce = new Accessory();
			acce.setIs_del(0);
			acce.setFile_level(SysConstant.SUB_IMG);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", productInfo2.getProduct_id());
			acce = super.getFacade().getAccessoryService().getAccessory(acce);
			productInfo2.setAccessory(acce);
		}
		productInfo.getMap().put("type1_id", type1_id);
		productInfo.getMap().put("type2_id", type2_id);
		productInfo.getMap().put("type1_name", type1_name);
		productInfo.getMap().put("type2_name", type2_name);
		productInfo.getMap().put("is_recommend", is_recommend);
		productInfo.getMap().put("title", title);
		request.setAttribute("entityList", list);
		request.setAttribute("title", title);

		return mapping.findForward("list");
	}

	/**
	 * 获取产品一级类别
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getProductType1(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		SysDictionary sd = new SysDictionary();
		sd.setDictionary_id(SysConstant.PRODUCT_FIRST_TYPE);
		sd.setIs_del(0);
		List<SysDictionary> stList = super.getFacade().getSysDictionaryService().getSysDictionaryList(sd);

		List<String> list = new ArrayList<String>();

		StringBuffer sf = new StringBuffer("[");
		if (null != stList && stList.size() > 0) {
			for (SysDictionary sysDictionary : stList) {
				String str = "{'id':'" + sysDictionary.getDictionary_value() + "','name':'"
						+ sysDictionary.getDictionary_desc_cn() + "'}";
				list.add(str);
			}
			sf.append(StringUtils.join(list, ","));
		}
		sf.append("]");
		logger.info("#########:{}", sf.toString());
		super.renderJson(response, sf.toString());
		return null;
	}

	/**
	 * 显示产品展示列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward orderSelect(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// SysDictionary 查询一级 0 PRODUCT_FIRST_TYPE 4000
		// 根据一级查二级 par_id 一起封装LIST
		String par_id = "0";
		String dictionary_id = String.valueOf(SysConstant.PRODUCT_FIRST_TYPE);

		SysDictionary productType = new SysDictionary();
		productType.setPar_id(Long.valueOf(par_id));
		productType.setDictionary_id(Long.valueOf(dictionary_id));
		productType.setIs_del(0);

		List<SysDictionary> typeList = super.getFacade().getSysDictionaryService().getSysDictionaryList(productType);
		for (SysDictionary sysDictionary : typeList) {
			Long id = sysDictionary.getId();
			SysDictionary secondProductType = new SysDictionary();
			secondProductType.setPar_id(id);
			secondProductType.setDictionary_id(SysConstant.PRODUCT_SECOND_TYPE);
			secondProductType.setIs_del(0);
			List<SysDictionary> secondTypeList = super.getFacade().getSysDictionaryService().getSysDictionaryList(
					secondProductType);
			sysDictionary.setSysDictionaryList(secondTypeList);
		}
		request.setAttribute("typeList", typeList);

		// 根据二级查询//ProductInfo 产品型号名称 及图片信息
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		String type1_id = (String) dynaBean.get("type1_id");
		String type2_id = (String) dynaBean.get("type2_id");
		String type1_name = (String) dynaBean.get("type1_name");
		String type2_name = (String) dynaBean.get("type2_name");
		String is_recommend = (String) dynaBean.get("is_recommend");
		String title = (String) dynaBean.get("title");
		Pager pager = (Pager) dynaBean.get("pager");
		pager.setPageSize(9);
		ProductInfo productInfo = new ProductInfo();
		productInfo.setIs_del(0);
		if (StringUtils.isNotBlank(is_recommend) && "1".equals(is_recommend)) {
			productInfo.setIs_recommend(Integer.parseInt(is_recommend));
			title = "<a title=\"店长推荐产品\" href=\"ProductDisplay.do?method=orderSelect&mod_id=3200&is_recommend=1\">店长推荐产品</a>";
		} else if (StringUtils.isNotBlank(type1_id) && StringUtils.isBlank(type2_id)) {
			productInfo.getMap().put("type1_id", type1_id);
			title = "<a title='" + type1_name + "' href='ProductDisplay.do?method=orderSelect&mod_id=3200&type1_id="
					+ type1_id + "&type1_name=" + type1_name + "'>" + type1_name + "</a>";
		} else if (StringUtils.isNotBlank(type1_id) && StringUtils.isNotBlank(type2_id)) {
			productInfo.setProduct_type(Integer.parseInt(type2_id));
			title = "<a title='" + type1_name + "' href='ProductDisplay.do?method=orderSelect&mod_id=3200&type1_id="
					+ type1_id + "&type1_name=" + type1_name + "'>" + type1_name + "</a>&gt;&gt;<a title='"
					+ type2_name + "' href='ProductDisplay.do?method=list&mod_id=3200&type1_id=" + type1_id
					+ "&type1_name=" + type1_name + "&type2_id=" + type2_id + "&type2_name=" + type2_name + "'>"
					+ type2_name + "</a>";
		}
		// super.copyProperties(productInfo, form);

		Long recordCount = super.getFacade().getProductInfoService().getProductInfoCount(productInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		productInfo.getRow().setFirst(pager.getFirstRow());
		productInfo.getRow().setCount(pager.getLastRow());
		List<ProductInfo> list = super.getFacade().getProductInfoService().getProductInfoPaginatedList(productInfo);
		for (ProductInfo productInfo2 : list) {
			Accessory acce = new Accessory();
			acce.setIs_del(0);
			acce.setFile_level(SysConstant.SUB_IMG);
			acce.getMap().put("mod_id", mod_id);
			acce.getMap().put("record_id", productInfo2.getProduct_id());
			acce = super.getFacade().getAccessoryService().getAccessory(acce);
			productInfo2.setAccessory(acce);
		}
		productInfo.getMap().put("type1_id", type1_id);
		productInfo.getMap().put("type2_id", type2_id);
		productInfo.getMap().put("type1_name", type1_name);
		productInfo.getMap().put("type2_name", type2_name);
		productInfo.getMap().put("is_recommend", is_recommend);
		productInfo.getMap().put("title", title);
		request.setAttribute("entityList", list);
		request.setAttribute("title", title);

		return new ActionForward("/hf/ProductDisplay/orderSelect.jsp");
	}
}